function fix0(v) {
    return (v <= 9) ? ('0' + v) : v;
}
/**
 * timestamp to yyyy-mm-dd hh:mm:ss
 * @param  {[type]} t         [timestamp]
 * @param  {[type]} date_only [1 -> yyyy-mm-dd]
 */
function conv_time(t, date_only) {
    // var d = new Date( Number(t) );
    if (!t || t.toString().indexOf('-') >= 0) return t;
    var d = new Date(t);
    var date = d.getFullYear() + '-' +
        fix0(d.getMonth() + 1) + '-' +
        fix0(d.getDate());
    var time = fix0(d.getHours()) + ':' +
        fix0(d.getMinutes()) + ':' +
        fix0(d.getSeconds());
    // time = time.slice(0, time.indexOf("."));
    return date_only ? date : (date + ' ' + time);
}

function timestamp(data, fields, date_only) {
    if (data instanceof Array) {
        for (var i in data) {
            for (var j in fields) {
                data[i][fields[j]] = conv_time(data[i][fields[j]], date_only);
            }
        }
    } else {
        return conv_time(data, !!fields)
    }
}

/**
 * 获取地址栏参数
 */
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    return r != null ? decodeURI(r[2]) : null;
}

/**
 * 获取字节长度。
 *
 * @param      {string}           str     The string
 * @return     int
 */
function getByteLength(str) {
    // 把双字节的替换成两个单字节的然后再获得长度 
    if (str == null) return 0;
    if (typeof str != "string") {
        str += "";
    }
    return str.replace(/[^\x00-\xff]/g, "01").length;
    /* 循环统计，双字节长度为2.
    var b = 0;
    for (var i = 0, l = str.length; i < l; i++) {
        if (/^[\u0000-\u00ff]$/.test(str.charCodeAt(i))) {
            b++;
        } else {
            b += 2;
        }
    }
    return b;*/
}

/**
 * 字符串是否为空
 *
 * @param      {<type>}   str     The string
 * @return     {boolean}  { description_of_the_return_value }
 */
function stringIsNull(str) {
    if (null == str || (typeof str == "undefined")) {
        return true;
    }
    if (typeof str == "string" && str.length > 0) {
        return false;
    }
    return true;
}

function stringIsNotNull(str) {
    return !stringIsNull(str);
}